package com.atwisdom.star.core.dao.pojo.modelClass;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import jodd.util.StringUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class BaseModel extends BaseTable {
    /**
     * 属性信息
     */
    @TableField(value = "properties", typeHandler = FastjsonTypeHandler.class)
    public JSONArray properties;

    /**
     * 属性信息
     */
    @TableField(value = "statusProperties", typeHandler = FastjsonTypeHandler.class)
    public JSONArray statusProperties;

    /**
     * 获取默认values
      * @return 返回结果
     */
    public JSONObject fetchValues() {
        JSONObject values = new JSONObject();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            values.put(itemJson.getString("name"), itemJson.get("defaultValue"));
        }
        return values;
    }

    /**
     * 获取非果询属values
     * @return 返回结果
     */
    public JSONObject fetchNonFeatureValues() {
        JSONObject values = new JSONObject();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isBlank(itemJson.getString("code"))) {
                values.put(itemJson.getString("name"), itemJson.get("defaultValue"));
            }
        }
        return values;
    }


    /**
     * 获取查询属性 featureProperties
     * @return 返回结果
     */
    public List<JSONObject> fetchFeatureProperties() {
        List<JSONObject> featureProperties = new ArrayList<>();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isNotBlank(itemJson.getString("code"))) {
                featureProperties.add(itemJson);
            }
        }
        Comparator<JSONObject> compByLength = (j1, j2) -> j2.getString("name").length() - j1.getString("name").length();

        return featureProperties.stream().sorted(compByLength).collect(Collectors.toList());
    }

    /**
     * 获取查询默认values
     * @return 返回结果
     */
    public JSONObject fetchFeatures() {
        JSONObject features = new JSONObject();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isNotBlank(itemJson.getString("code"))) {
                features.put(itemJson.getString("code"), itemJson.get("defaultValue"));
            }
        }
        return features;
    }

    public JSONObject fetchFeaturesByName(String name){
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            String fieldName = itemJson.getString("name");
            if(fieldName.equalsIgnoreCase(name) && StringUtil.isNotBlank(itemJson.getString("code"))){
                return itemJson;
            }
        }
        return null;
    }

    public JSONObject fetchPropertiesByName(String name){
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            String fieldName = itemJson.getString("name");
            if(fieldName.equalsIgnoreCase(name)){
                return itemJson;
            }
        }
        return null;
    }

    /**
     * 获取非查询属性 nonFeaturesProperties;
     * @return 返回结果
     */
    public List<JSONObject> fetchNonFeaturesProperties() {
        List<JSONObject> nonFeaturesProperties = new ArrayList<>();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isBlank(itemJson.getString("code"))) {
                nonFeaturesProperties.add(itemJson);
            }
        }
        return nonFeaturesProperties;
    }

    /**
     * 获取非查询默认values
     * @return 返回结果
     */
    public JSONObject fetchNonFeatures() {
        JSONObject nonFeatures = new JSONObject();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isBlank(itemJson.getString("code"))) {
                nonFeatures.put(itemJson.getString("name"), itemJson.get("defaultValue"));
            }
        }
        return nonFeatures;
    }

    /**
     * 获取查询组
     * @return 返回结果
     */
    public JSONArray fetchGroups() {
        JSONArray groups = new JSONArray();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            groups.add(itemJson.getString("group"));
        }
        return groups;
    }

    /**
     * 获取组属性
     * @param prop 组名
     * @return 返回结果
     */
    public JSONArray fetchGroupProperties(String prop) {
        return (JSONArray) this.properties.stream()
                .filter(item -> {
                    JSONObject itemJson = JsonUtil.toJSONObject(item);
                    return prop.equals(itemJson.getString("group"));
                });
    }

    /**
     * 获取组values
     * @param prop 组名
     * @return 返回结果
     */
    public JSONObject fetchGroupValues(String prop) {
        JSONObject values = new JSONObject();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);

            if (prop.equals(itemJson.getString("group"))) {
                values.put(itemJson.getString("name"), itemJson.get("defaultValue"));
            }
        }
        return values;
    }

}
